<template>
    <el-col :span="24">
        <el-tabs type="border-card" class="table-card" id="tab-card">
            <el-tab-pane label="用户列表">
                <div class="tableContent">
                    <el-button type="danger" class="top-btn batch-remove-btn" @click="batchRemove"
                               :disabled="this.sels.length===0">批量删除
                    </el-button>

                    <div class="top-btn" @click="handleAdd">新增</div>
                    <section :style="{height: autoHeight}">
                        <!--列表-->
                        <el-table :data="users" highlight-current-row v-loading="listLoading"
                                  @selection-change="selsChange" :height="tableHeight"
                                  :max-height="tableHeight">
                            <el-table-column type="selection" width="105">
                            </el-table-column>
                            <!--<el-table-column prop="id" label="用户ID"></el-table-column>-->
                            <el-table-column prop="name" label="姓名"></el-table-column>
                            <el-table-column prop="sex" label="性别"></el-table-column>
                            <el-table-column prop="age" label="年龄"></el-table-column>
                            <el-table-column prop="birth" label="出生日期"></el-table-column>
                            <el-table-column prop="addr" label="家庭住址"></el-table-column>

                            <el-table-column label="操作">
                                <template slot-scope="scope">
                                    <el-button type="primary" class="edit-btn img-btn"
                                               @click="handleEdit(scope.$index, scope.row)"></el-button>
                                    <el-button type="primary" class="del-btn img-btn"
                                               @click="handleDel(scope.$index, scope.row)"></el-button>
                                </template>
                            </el-table-column>
                        </el-table>

                        <!--工具条-->
                        <el-col :span="24" class="toolbar">
                            <el-pagination
                                    @size-change="handleSizeChange"
                                    @current-change="handleCurrentChange"
                                    :current-page="currentPage"
                                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                                    :page-size="pageSize"
                                    layout="total, sizes, prev, pager, next"
                                    :total="totalPageCount">
                            </el-pagination>
                        </el-col>

                        <!--编辑界面-->
                        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
                            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                                <el-form-item label="ID" prop="id">
                                    <el-input v-model="editForm.id" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="姓名" prop="name">
                                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="性别" prop="sex">
                                    <el-input v-model="editForm.sex" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="年龄" prop="age">
                                    <el-input v-model="editForm.age" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="出生日期" prop="birth">
                                    <el-input v-model="editForm.birth" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="家庭住址" prop="addr">
                                    <el-input v-model="editForm.addr" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click.native="editFormVisible = false">取消</el-button>
                                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交
                                </el-button>
                            </div>
                        </el-dialog>

                        <!--新增界面-->
                        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
                            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                                <el-form-item label="ID" prop="id">
                                    <el-input v-model="addForm.id" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="姓名" prop="name">
                                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="性别" prop="sex">
                                    <el-input v-model="addForm.sex" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="年龄" prop="age">
                                    <el-input v-model="addForm.age" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="出生日期" prop="birth">
                                    <el-input v-model="addForm.birth" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="家庭住址" prop="addr">
                                    <el-input v-model="addForm.addr" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click.native="addFormVisible = false">取消</el-button>
                                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                            </div>
                        </el-dialog>
                    </section>

                </div>
            </el-tab-pane>
            <el-tab-pane label="Java后台Users">
                <section :style="{height: autoHeight}">
                    <!--列表-->
                    <el-table :data="usersJava" highlight-current-row v-loading="listLoading"
                              @selection-change="selsChange" :height="tableHeight"
                              :max-height="tableHeight">
                        <el-table-column type="selection" width="105">
                        </el-table-column>
                        <el-table-column prop="admin_id" label="用户ID"></el-table-column>
                        <el-table-column prop="admin_name" label="姓名"></el-table-column>
                        <el-table-column prop="mail" label="邮箱"></el-table-column>
                        <el-table-column prop="role.identify" label="身份"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" class="edit-btn img-btn"
                                           @click="handleEdit(scope.$index, scope.row)"></el-button>
                                <el-button type="primary" class="del-btn img-btn"
                                           @click="handleDel(scope.$index, scope.row)"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </section>

            </el-tab-pane>
        </el-tabs>

    </el-col>
</template>

<script>
    import { getUserList, getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
    export default {
        data() {
            return {
                users: [],
                usersJava: [],
                usersPage: [],
                currentPage: 1,
                pageSize: 10,
                totalPageCount: 0,

                autoHeight: '',
                tableHeight: 0,

                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '姓名', trigger: 'blur'}
                    ],
                    sex: [
                        {required: true, message: '性别', trigger: 'blur'}
                    ],
                    age: [
                        {required: true, message: '年龄', trigger: 'blur'}
                    ],
                    birth: [
                        {required: true, message: '出生日期', trigger: 'blur'}
                    ],
                    addr: [
                        {required: true, message: '家庭住址', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                    name: '',
                    sex: '',
                    age: '',
                    birth: '',
                    addr: '',
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '姓名', trigger: 'blur'}
                    ],
                    sex: [
                        {required: true, message: '性别', trigger: 'blur'}
                    ],
                    age: [
                        {required: true, message: '年龄', trigger: 'blur'}
                    ],
                    birth: [
                        {required: true, message: '出生日期', trigger: 'blur'}
                    ],
                    addr: [
                        {required: true, message: '家庭住址', trigger: 'blur'}
                    ]
                },
                //新增界面数据
                addForm: {
                    id: 0,
                    name: '',
                    sex: '',
                    age: '',
                    birth: '',
                    addr: '',
                }
            }
        },
        methods: {
            //结合Java后台测试
            getUserForJava () {
              this.$http.get('/apis/SEMIS/admin/to_admin_list.action').then(res => {
                  alert(JSON.stringify(res.data.Records))
                  this.usersJava = res.data.Records;
              })
            },
            //获取用户列表
            getUserList() {
                // this.listLoading = true;
                getUserList('admin').then(res => {
                    for (let i = 0; i < res.data.users.length; i++){
                        res.data.users[i].age = res.data.users[i].age + ''
                    }
                    this.users = res.data.users;
                    // this.listLoading = false;
                });

                getUserListPage({page:this.currentPage,name:'admin'}).then(res => {
                    // alert(JSON.stringify(res.data.users))
                    this.usersPage = res.data.users;
                    // this.listLoading = false;
                });
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.getUserList();
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getUserList();
                console.log(`当前页: ${val}`);
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    removeUser(row.id).then(response => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        // this.getUserList();
                    }, response => {
                        this.$alert(response.body.errorMsg || '网络错误，请检查网络连接', {
                            confirmButtonText: '确定',
                        });
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    id: 0,
                    name: '',
                    sex: '',
                    age: '',
                    birth: '',
                    addr: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            editUser({
                                    id: this.editForm.id,
                                    name: this.editForm.name,
                                    sex: this.editForm.sex,
                                    age: this.editForm.age,
                                    birth: this.editForm.birth,
                                    addr: this.editForm.addr
                                },
                            ).then(response => {
                                this.editLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getUserList();
                            }, response => {
                                this.$alert(response.body.errorMsg || '网络错误，请检查网络连接', {
                                    confirmButtonText: '确定',
                                });
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            addUser({
                                    id: this.addForm.id,
                                    name: this.addForm.name,
                                    age: this.addForm.SSCageSMC,
                                    sex: this.addForm.sex,
                                    birth: this.addForm.birth,
                                    addr: this.addForm.addr
                                },
                            ).then(response => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                // this.getUserList();
                            }, response => {
                                this.$alert(response.body.errorMsg || '网络错误，请检查网络连接', {
                                    confirmButtonText: '确定',
                                });
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    batchRemoveUser(ids).then(response => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUserList();
                    }, response => {
                        this.$alert(response.body.errorMsg || '网络错误，请检查网络连接', {
                            confirmButtonText: '确定',
                        });
                    });
                }).catch(() => {

                });
            },
        },
        mounted() {
            let height = document.getElementById("tab-card").offsetHeight;
            this.autoHeight = height - 95 + 'px';
            this.tableHeight = height - 95;
            let that = this;
            window.onresize = () => {
                this.autoHeight = height - 95 + 'px';
                that.tableHeight = document.body.clientHeight - 95
            };
            this.getUserList();
            this.getUserForJava()
        }
    }
</script>